<?php
	include_once("BdMysql.php");
	class BD{
		protected $bd_conn;
		
		 public function __construct()
		 {
			$this->bd_conn = new  BdMysql();
		 }
		
		function do_login($user, $pass)
		{
			//$query = $this->bd_conn->queryLogin();
			$query = sprintf($this->bd_conn->queryLogin(),mysql_real_escape_string($user),mysql_real_escape_string($pass));
			$id_usu = $this->bd_conn->executeQuery1String($query);
			if ($id_usu['id'] != null && $id_usu['nombre'] != null )
				return array('id' => $id_usu['id'],'nombre' => $id_usu['nombre']);
			else
				return "";
		}
		
		function getDocumentos()
		{
			$query = $this->bd_conn->queryDocumentos();
			$rdocs = $this->bd_conn->executeQueryString($query);
			$docs;
			for($i=0; $i < sizeof($rdocs) ; $i++ )
			{
				$docs[$rdocs[$i]['id']] = $rdocs[$i]['nombre'];
			}			
			return $docs;
		}
		
		function getCapitulos($id_doc)
		{
			$caps = array();
			$query = sprintf($this->bd_conn->queryCapitulos(),mysql_real_escape_string($id_doc));
			
			$rcaps = $this->bd_conn->executeQueryString($query);
			for($i=0;$i < sizeof($rcaps) ; $i++)
			{
				$caps[$rcaps[$i]['id']] = array( "nombre" => $rcaps[$i]['nombre'], "estado" => $rcaps[$i]['estado'], "id" => $rcaps[$i]['id']);
			}
			return $caps;		
		}
		
		function getUsuarios($id_doc)
		{
			$usus = array();
			$query = sprintf($this->bd_conn->queryPermisos(),mysql_real_escape_string($id_doc));
			
			$rusus = $this->bd_conn->executeQueryString($query);
			
			for($i=0;$i < sizeof($rusus) ; $i++)
			{
				$usus[$i] = array( "nombre" => $rusus[$i]['nombre'], "rol" => $rusus[$i]['rol']);
			}
			return $usus;		
		}
		
		function getFrasesOriginales($id_cap)
		{
			$query = sprintf($this->bd_conn->queryFrasesOriginales(),mysql_real_escape_string($id_cap));
			return $this->getFrasesMeta($id_cap,$query);
		}
		
		function getFrases($id_cap)
		{
			$query = sprintf($this->bd_conn->queryFrases(),mysql_real_escape_string($id_cap));
			return $this->getFrasesMeta($id_cap,$query);
		}

		function getFrasesMeta($id_cap,$query)
		{
			$frases = array();
			//$query = sprintf($this->bd_conn->queryFrases(),mysql_real_escape_string($id_cap));
			$rfrases = $this->bd_conn->executeQueryString($query);
			
			for($i=0;$i < sizeof($rfrases) ; $i++)
			{
				$frases[$i] = array( "id" => $rfrases[$i]['id'], "contenido" => $rfrases[$i]['contenido']);
			}
			return $frases;		
		}	
		
		function getTemplate($id_cap)
		{
			$query = sprintf($this->bd_conn->queryTemplate() , mysql_real_escape_string($id_cap));
			$rtemplate = $this->bd_conn->executeQuery1String($query);
			return $rtemplate['template'];
		}
		
		function getDiccionario($id_doc)
		{
			$glosario = array();
			$query = sprintf($this->bd_conn->queryGlosario() , mysql_real_escape_string($id_doc));
			$rglosario = $this->bd_conn->executeQueryString($query);
			
			for($i=0; $i < sizeof($rglosario) ; $i++)
			{
				$glosario[$i] = array ("clave" => $rglosario[$i]['clave'], "valor" => $rglosario[$i]['valor']);
			}
			return $glosario;
		}
	}
?>